package com.maikang.exammall.task.async;

import com.maikang.exammall.configuration.property.ApplicationProperty;
import com.maikang.exammall.entity.Order;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@RequiredArgsConstructor
@Log4j2
public class WechatMessageTemplate {
    private final WxMpService wxMpService;
    private final ApplicationProperty applicationProperty;

    // 模板消息字体颜色
    private static final String TEMPLATE_FRONT_COLOR = "#32CD32";
    private static final String BLUE = "#000093";
    private static final String ORANGE = "#642100";
    private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Async("threadPoolTaskExecutor")
    public void NotifyOperatorsAfterOrderRefundIsApplied(Order order) {
        /**
         *  发起退款时通知运营人员
         */
        log.warn("NotifyOperatorsAfterOrderRefundIsApplied is running...");

        try {
            Thread.sleep(1000*5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.warn("NotifyOperatorsAfterOrderRefundIsApplied sleep done. ");
        final String templateId = "G5Rz2Ess-YYF6oomsw7JQQ5Et2GVHz5eOOxVJoCMEnY";
        List<String> openIds = applicationProperty.getOrderRefundAppliedNotifyToOperatorOpenIds();
        for (String openId : openIds) {
            WxMpTemplateMessage orderRefundAppliedTemplate = WxMpTemplateMessage.builder()
                    .toUser(openId)
                    .templateId(templateId)
                    .url("")
                    .build();
            WxMpTemplateData first = new WxMpTemplateData("first", "平台有退款申请， 请您进入后台完成审核");
            WxMpTemplateData keyword1 = new WxMpTemplateData("keyword1", order.getOutTradeNo(), BLUE);
            WxMpTemplateData remark = new WxMpTemplateData("remark", "请进入后台处理， 申请时间: " + df.format(new Date()), ORANGE);
            orderRefundAppliedTemplate.addData(first)
                    .addData(keyword1)
                    .addData(remark);
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(orderRefundAppliedTemplate);
            } catch (Exception e) {
                log.error("fail to NotifyOperatorsAfterOrderRefundIsApplied, openIds: {}, order: {}, error: {}", openId, order, e.getMessage());
            }
        }
    }

    @Async("threadPoolTaskExecutor")
    public void NotifyOperatorsAfterOrderIsPaid(Order order) {
        /**
         * 下单成功后微信推送给员工 use the 2 nd
         */
        final String templateId = "102fXlDTbJTx_RqhdLNh7KVZNJJOfbWo2AiwwtuA9A4";
        List<String> openIds = applicationProperty.getOrderRefundAppliedNotifyToOperatorOpenIds();
        for (String openId : openIds) {
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(openId)
                    .templateId(templateId)
                    .url("").build();
            WxMpTemplateData first = new WxMpTemplateData("first", "您有新的订单，请在后台管理中心处理该订单", TEMPLATE_FRONT_COLOR);
            // 订单号
            WxMpTemplateData keyword1 = new WxMpTemplateData("keyword1", order.getOutTradeNo(), BLUE);
            // 实付金额
            WxMpTemplateData keyword2 = new WxMpTemplateData("keyword2", order.getAmount()/100.0 + " 元", ORANGE);
            // 下单时间
            WxMpTemplateData keyword3 = new WxMpTemplateData("keyword3",  df.format(new Date()));
            WxMpTemplateData remark = new WxMpTemplateData("remark", "请尽快处理");

            templateMessage.addData(first)
                    .addData(keyword1)
                    .addData(keyword2)
                    .addData(keyword3)
                    .addData(remark);
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (Exception e) {
                log.error("fail to NotifyOperatorsAfterOrderIsPaid, openIds: {}, order: {}, error: {}", openId, order, e.getMessage());
            }
        }
    }

    @Async("threadPoolTaskExecutor")
    public void NotifyClientAfterOrderIsPaid(Order order) {

        /**
         * 付款成功后推送给客户 give up the 3rd , use the 6th
         */
        final String templateId = "jIWVI8mZj7C_v_PscxgHB1MslRApfe_yE0q1ScXQgZ0";
        final String url = String.format("https://www.mkhealth.club/#/orderDetails?id=%d", order.getId());
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(order.getOpenId())
                .templateId(templateId)
                .url(url).build();

        WxMpTemplateData first = new WxMpTemplateData("first", "您好，您的体检套餐已成功下单!");
        WxMpTemplateData keyword1 = new WxMpTemplateData("keyword1", order.getOutTradeNo(), BLUE);
        WxMpTemplateData keyword2 = new WxMpTemplateData("keyword2", df.format(order.getLastModifiedDate()));
        WxMpTemplateData keyword3 = new WxMpTemplateData("keyword3", "体检");
        WxMpTemplateData keyword4 = new WxMpTemplateData("keyword4", order.getAmount()/100.0 + " 元");
        WxMpTemplateData remark = new WxMpTemplateData("remark", "温馨提示：您的订单信息客服会在一个工作日内与您电话核实，请您保持电话通畅。如有疑问请拨打客服热线0668-2853837。祝您身体健康，幸福美满！", ORANGE);
        templateMessage.addData(first)
                .addData(keyword1)
                .addData(keyword2)
                .addData(keyword3)
                .addData(keyword4)
                .addData(remark);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            log.error("fail to NotifyClientAfterOrderIsPaid, order: {}, error: {}", order, e.getMessage());
        }
    }

//    public void NotifyClientAfterAppointmentIsMade(String openId, Date examDate, )
}
